package com.cindata

import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*
import grails.gorm.transactions.Transactional
@Secured(['isAuthenticated()'])
@Transactional(readOnly = true)
class ContractController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Contract.list(params), model:[contractCount: Contract.count()]
    }

    def show(Contract contract) {
        respond contract
    }

    def create() {
        respond new Contract(params)
    }

    def related() {
    }

    @Transactional
    def doRelated(){
        def mortgegeId = params['mortgageId']
        def contractId = params['contract']
        if(!mortgegeId || !contractId){
            flash.message = "请选择关联合同"
            redirect action: "related",params:[mortgageId:mortgegeId]
            return
        }
        //如果贷款合同号无效 不处理
        def mortgage = Mortgage.findById(Long.parseLong(mortgegeId))
        def contract = Contract.findById(Long.parseLong(contractId))
        if(mortgage == null || contract == null){
            return
        }

        def mortgageContract = new MortgageContract()
        mortgageContract.contract = contract
        mortgageContract.mortgage = mortgage
        mortgageContract.save flush:true
        redirect action:"show" ,controller:"mortgage",params:[id:mortgage?.id]
    }

    @Transactional
    def save(Contract contract) {
        def mortgege_id = params['mortgageId']

        //如果贷款合同号无效 不处理
        def mortgage = Mortgage.findById(Long.parseLong(mortgege_id))
        if(mortgage == null){
            return
        }

        if (contract.hasErrors()) {
            println contract.errors
            transactionStatus.setRollbackOnly()
            respond contract.errors, view:'create'
            return
        }

        contract.save flush:true

        def mortgageContract = new MortgageContract()
        mortgageContract.contract = contract
        mortgageContract.mortgage = mortgage
        mortgageContract.save flush:true

        redirect action:"show" ,controller:"mortgage",params:[id:mortgage?.id]
    }

    def edit(Contract contract) {
        respond contract
    }

    @Transactional
    def updateContract(Contract contract){
        def mortgege_id = params['mortgageId']

        //如果贷款合同号无效 不处理
        def mortgage = Mortgage.findById(Long.parseLong(mortgege_id))
        if(mortgage == null){
            return
        }

        if (contract == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (contract.hasErrors()) {
            println contract.errors
            transactionStatus.setRollbackOnly()
            respond contract.errors, view:'create'
            return
        }

        contract.save flush:true

        redirect action:"show" ,controller:"mortgage",params:[id:mortgage?.id]
    }

    @Transactional
    def update(Contract contract) {
        if (contract == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (contract.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond contract.errors, view:'edit'
            return
        }

        contract.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'contract.label', default: 'Contract'), contract.id])
                redirect contract
            }
            '*'{ respond contract, [status: OK] }
        }
    }

    @Transactional
    def delete(Contract contract) {
        def mortgageId = params['mortgageId']
        def mortgage = Mortgage.findById(Long.parseLong(mortgageId))
        def mortgageContract = MortgageContract.findByMortgageAndContract(mortgage,contract)
         if(!mortgageContract){
            println "not find"
             return
         }
        mortgageContract.delete flush:true
        //contract.delete flush:true

        redirect action:"show" ,controller:"mortgage",params:[id:mortgage?.id]
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'contract.label', default: 'Contract'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
